<template>
  <view>
    <nav-bar title="编辑地址" backgroundColor="#115BAA" titleColor="#FFF"></nav-bar>
    <scroll-view scroll-y :style="{ height: scrollHeight }">
      <view class="content-box">
        <view class="form-box">
          <you-form
            ref="form"
            :showFormTitle="false"
            :formConfig="addressConfig"
            v-model="addressData"
          ></you-form>
          <view class="default-box" @click="clickDefault">
            <uni-icons class="icon" v-if="chooseDefault" type="checkbox-filled" color="#2A82E4" size="18"></uni-icons>
            <uni-icons class="icon" v-else type="checkbox" color="#999" size="18"></uni-icons>
            设为默认地址
          </view>
        </view>
      </view>
    </scroll-view>
    <view class="form_bottom_btn">
      <view class="btn-box">
        <view class="confirm" @click="clickConfirmEdit">确认编辑</view>
      </view>
    </view>
  </view>
</template>

<script>
  import api_user from '@/api/user'
  import { ADDRESS_FORM } from '../formConfig/addressConfig'
  export default {
    name: 'EditAddress',
    data() {
      return {
        scrollHeight: '',
        addressConfig: [],
        addressData: {},
        chooseDefault: false,
        addressId: ''
      }
    },
    onLoad(options) {
      this.addressId = options.id
      this.scrollHeight = this.$store.getters.heightData.noTabMainHeight - 66 + 'px'
      this.addressConfig = uni.$u.deepClone(ADDRESS_FORM)
      this.getAddressDetail()
    },
    methods: {
      getAddressDetail() {
        const params = {
          id: this.addressId
        }
        api_user.fetchAddressDetail(params).then(res => {
          if (res.code !== 200) return
          this.addressData = res.data
        })
      },
      clickDefault() {
        this.chooseDefault = !this.chooseDefault
      },
      clickConfirmEdit() {
        const params = {
          id: this.addressId,
          ...this.addressData,
          isDefault: this.chooseDefault ? 1 : 2
        }
        api_user.editAddress(params).then(res => {
          if (res.code !== 200) return

        })
        uni.redirectTo({
          url: `/subPages/userIndex/rehabilitationAids/goodsAddress`
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .content-box {
    padding-top: 30rpx;
    .form-box {
      margin: 0 30rpx;
      border-radius: 16rpx;
      background-color: #FFF;
      overflow: hidden;
      .default-box {
        display: flex;
        align-items: center;
        padding: 0 30rpx;
        min-height: 90rpx;
        font-size: 28rpx;
        color: #606266;
        .icon {
          margin-right: 10rpx;
        }
      }
    }
  }
</style>
